#include "Components/ComponentCreature.h"
#include "Components/ComponentBody.h"
#include "Components/ComponentHealth.h"
#include "Components/ComponentLocomotion.h"
#include "Components/ComponentCreatureModel.h"
#include "Components/ComponentHumanModel.h"

#include "GameSingleton.h"
#include "GameRegistry.hpp"
#include "Game/TableComponent.hpp"
#include "Log.h"

#include <exception>


namespace PocketSurvival
{


    
    static const std::string componentName = "Creature";
    const std::string &ComponentCreature::getName() const
    {
        return componentName;
    }
    const std::string &ComponentCreature::GetName()
    {
        return componentName;
    }

    void ComponentCreature::load(const nlohmann::json &jsonData)
    {
        std::vector<ComponentCreatureModel*> modelVec;
        TableComponent *tableCom = GameSingleton::gameRegistry->get<TableComponent*>(entity);
        tableCom->dynamicFindComponent<ComponentCreatureModel>(modelVec);
        if (modelVec.size() > 0)
        {
            if (modelVec.size() > 1)
            {
                Log::Error(fmt::format("出现多个 ComponentCreatureModel 的问题"));
            }
            componentCreatureModel = modelVec[0];
        }

        componentBody = tableCom->findComponent<ComponentBody>();
        componentHealth = tableCom->findComponent<ComponentHealth>();
        componentLocomotion = tableCom->findComponent<ComponentLocomotion>();
        componentSpawn = tableCom->findComponent<ComponentSpawn>();
        componentCreatureSounds = tableCom->findComponent<ComponentCreatureSounds>();


        constantSpawn = jsonData["ConstantSpawn"].get<bool>();
        category = (CreatureCategory)jsonData["Category"].get<uint8_t>();
        displayName = jsonData["DisplayName"].get<std::string>();
    }

    void ComponentCreature::save(nlohmann::json &jsonData)
    {
        jsonData["ConstantSpawn"] = constantSpawn;
    }

    void ComponentCreature::saveToMsgJson(nlohmann::json &jsonData)
    {
        MSG_VALUE_SET(jsonData, "ConstantSpawn", "bool", constantSpawn);
    }
} // namespace PocketSurvival
